package com.ruoyi.project.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.domain.ResultBean;
import com.ruoyi.project.purchase.domain.RyDepotDetail;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.purchase.domain.RyPurchaseIn;
import com.ruoyi.project.purchase.service.IRyPurchaseInService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 采购入库Controller
 *
 * @author you
 * @date 2022-09-26
 */
@RestController
@RequestMapping("/purchase/ryPurchaseIn")
public class RyPurchaseInController extends BaseController
{
    @Autowired
    private IRyPurchaseInService ryPurchaseInService;

    /**
     * 查询采购入库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyPurchaseIn ryPurchaseIn)
    {
        startPage();
        ryPurchaseIn.setType("入库");
        ryPurchaseIn.setSubType("采购");
        List<RyPurchaseIn> list = ryPurchaseInService.selectRyPurchaseInList(ryPurchaseIn);
        return getDataTable(list);
    }

    /**
     * --采购入库
     * 新增采购入库信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
//        System.out.println("depotHeadItem---->" + depotHeadItem);
        System.out.println("linkHeadId---->" + depotHeadItem.getLinkHeadId());
        return ryPurchaseInService.addDepotHeadAndDetail(depotHeadItem);
    }


    /**
     * --采购入库
     * 获取单据主表详细信息
     */
    @GetMapping(value = "/selectDetailByDepotId/{id}")
    public AjaxResult selectDetailByDepotId(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryPurchaseInService.selectRyDepotDetailByDepotId(id));
    }



    /**
     * --采购入库
     * 根据主表修改单据子表信息
     */
    @RequestMapping("/editPurchaseInDetail")
    public AjaxResult editPurchaseInDetail(@RequestBody DepotHeadItem depotHeadItem){
        System.out.println("depotHeadItem---->" + depotHeadItem);
        return ryPurchaseInService.editDepotDetail(depotHeadItem);
    }

    /**
     * --采购入库
     * 单条数据入库
     */
    @RequestMapping("/insertOneDetail")
    public AjaxResult insertOneDetail(@RequestBody RyDepotDetail ryDepotDetail){
        System.out.println("ryDepotDetail---->" + ryDepotDetail);
        return ryPurchaseInService.insertOneDetail(ryDepotDetail);
    }

    /**
     * 新增采购入库单时关联采购订单id查询子表信息
     */
    @GetMapping("/selectItemForInByDepotId")
    public AjaxResult selectItemForInByDepotId(ParamBean paramBean){

        return AjaxResult.success(ryPurchaseInService.selectItemForInByDepotId(paramBean.getLinkHeadId()));
    }













    /**
     * 导出采购入库列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:export')")
    @Log(title = "采购入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyPurchaseIn ryPurchaseIn)
    {
        List<RyPurchaseIn> list = ryPurchaseInService.selectRyPurchaseInList(ryPurchaseIn);
        ExcelUtil<RyPurchaseIn> util = new ExcelUtil<RyPurchaseIn>(RyPurchaseIn.class);
        util.exportExcel(response, list, "采购入库数据");
    }




    /**
     * 获取采购入库详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryPurchaseInService.selectRyPurchaseInById(id));
    }

    /**
     * 新增采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:add')")
    @Log(title = "采购入库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RyPurchaseIn ryPurchaseIn)
    {
        return toAjax(ryPurchaseInService.insertRyPurchaseIn(ryPurchaseIn));
    }

    /**
     * 修改采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:edit')")
    @Log(title = "采购入库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RyPurchaseIn ryPurchaseIn)
    {
        return toAjax(ryPurchaseInService.updateRyPurchaseIn(ryPurchaseIn));
    }

    /**
     * 删除采购入库
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryPurchaseIn:remove')")
    @Log(title = "采购入库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ryPurchaseInService.deleteRyPurchaseInByIds(ids));
    }
}
